package com.jp.factoy.service.impl;

import com.jp.factoy.dao.FactoryMapper;
import com.jp.factoy.pojo.*;
import com.jp.factoy.service.FactoryService;
import com.jp.factoy.util.AllRecords;
import com.jp.factoy.util.ResultBuildVo;
import com.jp.factoy.util.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/6/13.
 */
@Service
@Slf4j
public class FactoryServiceImpl implements FactoryService {
    @Autowired
    private FactoryMapper factoryMapper;
    @Override
    @Transactional
    public ResultVo addFactory(FactoryAddVo factoryAddVo) {
        //做验证 添加是进行验证
        //验证编码是否已经存在
        List<Factory> codeList = factoryMapper.queryFactoryCode(factoryAddVo.getFcode());
        if(!CollectionUtils.isEmpty(codeList)){
            return ResultBuildVo.error("编码不能重复","500");
        }

        //给数据库的其他属性进行赋值
        Factory factory = new Factory();
        Date date = new Date();
        factory.setCreateTime(date);
        factory.setCreateName("小明");
        factory.setUpdateTime(date);
        factory.setUpdateName("小明");

        BeanUtils.copyProperties(factoryAddVo,factory);
        factoryMapper.addFactory(factory);
        return ResultBuildVo.success(factoryAddVo);
    }

    @Override
    public ResultVo updateFactory(FactoryUpdateVo factoryUpdateVo) {
        //给数据库的其他属性进行修改
        if(factoryUpdateVo.getState()==0){
            Factory factory = new Factory();
            Date date = new Date();
            factory.setUpdateTime(date);
            factory.setUpdateName("小黑土");

            BeanUtils.copyProperties(factoryUpdateVo,factory);
            factoryMapper.updateFactory(factory);
            return ResultBuildVo.success(factoryUpdateVo);
        }else {
            return ResultBuildVo.error("启动状态不能修改","500");
        }

    }

    @Override
    public ResultVo updateState(FactoryStateVo factoryStateVo) {
        if(factoryStateVo.getState()==0){
            factoryStateVo.setState(1);
            //给数据库的其他属性进行修改
            Factory factory = new Factory();
            Date date = new Date();
            factory.setUpdateTime(date);
            factory.setUpdateName("小明");

            BeanUtils.copyProperties(factoryStateVo,factory);
            factoryMapper.updateState(factory);
            return ResultBuildVo.success(factoryStateVo);
        }
        else {
            return ResultBuildVo.error("当前为启动状态，不能修改","500");
        }


    }

    @Override
    public ResultVo<Factory> queryFactoryById(Integer fid) {
        if(fid==null){
            return ResultBuildVo.error("查看是id不能为空","500");
        }
        Factory factory = factoryMapper.queryFactoryById(fid);
        if(factory==null){
            return  ResultBuildVo.error("产看的对象不存在","500");
        }
        return ResultBuildVo.success(factory);
    }

    @Override
    public ResultVo<AllRecords> queryPage(FactorySelectVo factorySelectVo) {
        //查询数据
        List<Factory> factoryList = factoryMapper.queryPage(factorySelectVo);
        //查询疏朗
        int count =  factoryMapper.queryCount(factorySelectVo);
        //当前页，每页大小，总条数，总页数，数据统一放到Allrecooreds里
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(factorySelectVo.getPageIndex());
        allRecords.setPageSize(factorySelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(factoryList);

        return ResultBuildVo.success(allRecords);

    }

}
